/**
 * Copyright � 2012 GestureSoft. ALL RIGHTS RESERVED.
 * This file contains proprietary and GestureSoft CONFIDENTIAL Information.
 * Use, disclosure or reproduction is prohibited.
 * 
 * @fileName GoogleService.as
 * @creationDate Aug 8, 2012
 * @author 
 * @version 1.0
 */
package com.gesturesoft.imultimax.socialize.google
{
	import com.adobe.serialization.json.JSONDecoder;
	import com.gesturesoft.imultimax.socialize.org.osflash.signals.Signal;
	
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;

	public class GoogleService
	{
		private var __authURL:String;
		private var __userInfoURL:String;
		private var __accessTokenURL:String;
		private var __clientID:String;
		private var __userInfo:Array;
		private var __appSecret:String;
		private var __accessToken:String;
		
		public var errorSignal:Signal = new Signal();
		public var userInfoReceived:Signal = new Signal();
		
		public function GoogleService()
		{
		}
		
		public function fetchAccessToken(code:String):void
		{
			var urlLoader:URLLoader = new URLLoader();
			urlLoader.addEventListener(Event.COMPLETE, onFetchAccesTokenComplete);
			
			var acURLStr:String = this.accessTokenURL;
			
			var urlVariable:URLVariables = new URLVariables();
			urlVariable.code = code;
			urlVariable.client_id = this.clientID;
			urlVariable.client_secret = this.appSecret;
			urlVariable.redirect_uri = "urn:ietf:wg:oauth:2.0:oob";
			urlVariable.grant_type= "authorization_code";
			
			var urlRequest:URLRequest = new URLRequest(acURLStr);
			urlRequest.data = urlVariable;
			urlRequest.method = URLRequestMethod.POST;
			
			try {
				urlLoader.load(urlRequest);
			} catch(err:Error) {
				errorSignal.dispatch(err);
			}
		}
		
		protected function onFetchAccesTokenComplete(event:Event):void
		{
			var rawData:String = "["+String(event.target.data)+"]";
			var atAr:Array = new JSONDecoder(rawData).getValue() as Array;
			this.accessToken = String(atAr[0].access_token);
			this.fetchUserInfo();
		}
		
		public function fetchUserInfo():void
		{
			var urlLoader:URLLoader = new URLLoader();
			urlLoader.addEventListener(Event.COMPLETE, onFetchUserInfoComplete);
			var urlRequest:URLRequest = new URLRequest(this.userInfoURL+this.accessToken);
			urlRequest.method = URLRequestMethod.GET;
			
			try {
				urlLoader.load(urlRequest);
			} catch(err:Error) {
				errorSignal.dispatch(err);
			}
		}
		
		protected function onFetchUserInfoComplete(event:Event):void
		{
			var rawData:String = "["+String(event.target.data)+"]";
			this.userInfo = new JSONDecoder(rawData).getValue() as Array;
			
			//this.userURL = this.graphURL+this.userInfo[0].id;
			
			userInfoReceived.dispatch(this.userInfo);
		}
		
		public function get authURL():String
		{
			return __authURL;
		}
		
		public function set authURL(val:String):void
		{
			__authURL = val;
		}
		
		
		public function get userInfoURL():String
		{
			return __userInfoURL;
		}
		
		public function set userInfoURL(val:String):void
		{
			__userInfoURL = val;
		}
		
		public function get accessTokenURL():String
		{
			return __accessTokenURL;
		}
		
		public function set accessTokenURL(val:String):void
		{
			__accessTokenURL = val;
		}
		
		public function get clientID():String
		{
			return __clientID;
		}
		
		public function set clientID(val:String):void
		{
			__clientID = val;
		}
		
		public function get userInfo():Array
		{
			return __userInfo;
		}
		
		public function set userInfo(val:Array):void
		{
			__userInfo = val;
		}
		
		public function get appSecret():String
		{
			return __appSecret;
		}
		
		public function set appSecret(val:String):void
		{
			__appSecret = val;
		}
		
		public function get accessToken():String
		{
			return __accessToken;
		}
		
		public function set accessToken(val:String):void
		{
			__accessToken = val;
		}
		
	}
}